<template>
  <div class="app-container">
    <div v-permission="['role:create']" class="addbtn" @click="dialogTop('add')">创建角色</div>
    <div class="seatch">
      <el-input
        v-model="listQuery.keyword"
        placeholder="按名称搜索"
        @keyup.enter.native="searchName($event)"
      >
        <i slot="prefix" class="el-input__icon el-icon-search" @click="getList()" />
      </el-input>
    </div>
    <div class="content">
      <el-table
        v-loading="listLoading"
        align="center"
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          align="center"
          label="角色名称"
        />
        <el-table-column
          prop="description"
          align="center"
          label="描述"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="创建时间"
        />
        <el-table-column
          align="center"
          prop="createAt"
          label="操作"
          width="220"
        >
          <template slot-scope="{row}">
            <div class="operate">
              <div v-permission="['role:edit']" @click="edgeDialog(row)">编辑</div>
              <div v-permission="['role:delete']" @click="deleteDialog(row)">删除</div>
              <div>
                <router-link :to="{ name: 'RoleLink', query: {id: row.id }}">关联权限</router-link>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageination-div">
        <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
      </div>
    </div>
    <!-- 添加,编辑角色 -->
    <el-dialog :title="diaDelOrAdd?'创建角色':'编辑角色'" :visible.sync="dialogFormVisible" :close-on-click-modal="false" custom-class="customClass">
      <el-form ref="addForm" :model="addForm" :rules="rules" auto-complete="on" label-position="left" style="width: 97%;">
        <el-form-item label="角色名称：" :label-width="formLabelWidth" prop="name">
          <el-input
            v-model="addForm.name"
            placeholder="请输入名称"
            name="name"
            maxlength="32"
            tabindex="1"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="描述：" :label-width="formLabelWidth" prop="description">
          <el-input
            v-model="addForm.description"
            type="textarea"
            :rows="4"
            resize="none"
            placeholder="请输入描述"
            name="description"
            maxlength="500"
            tabindex="2"
            auto-complete="off"
          />
        </el-form-item>
      </el-form>
      <div class="footer">
        <div class="cancel" @click="dialogTop('cancel')">
          <span>取消</span>
        </div>
        <div v-if="diaDelOrAdd" class="sure" @click="createData">
          <span>确认</span>
        </div>
        <div v-else class="sure" @click="updataData">
          <span>确认</span>
        </div>
      </div>
    </el-dialog>

    <delete-dialog
      :dislog-name="deleteObiect.name"
      :dialog-del-visible="dialogDelVisible"
      :title="'角色'"
      @funcancl="funcancl"
      @func="deleteData"
    />

  </div>
</template>

<script>
import Pagination from '@/components/pagination'
import DeleteDialog from '@/components/dialog/delete'
import { getRoleList, createRole, delRole, updateRole } from '@/api/role'
export default {
  name: 'RoleUser',
  components: {
    Pagination,
    DeleteDialog
  },
  data() {
    return {
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        keyword: null,
        pageNum: 1,
        pageSize: 10
      },
      dialogFormVisible: false,
      dialogDelVisible: false,
      formLabelWidth: '120px',
      lablePosition: 'left',
      diaDelOrAdd: false,
      addForm: {
        name: '',
        description: ''
      },
      deleteObiect: {},
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' }
        ],
        description: [
          { required: true, message: '必填', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    sizeChange(val) {
      this.pageNum = val
    },
    currentChange(val) {
      this.currentPage = val
    },
    searchName(e) {
      this.listQuery.keyword = e.target.value
      this.getList()
    },
    clean() {
      this.addForm = {
        name: '',
        description: ''
      }
    },
    funcancl(val) {
      this.dialogDelVisible = false
    },
    funsetcancl(val) {
      this.dialogResetVisible = false
    },
    // 获取数据
    async getList() {
      const res = await getRoleList(this.listQuery)
      if (res.code === 200) {
        this.tableData = res.data.list
        this.total = res.data.total || 0
        this.pages = res.data.pages || 0
        this.listLoading = false
      }
    },
    dialogTop(val, row) {
      if (val === 'cancel') {
        this.diaDelOrAdd = false
        this.dialogFormVisible = false
      } else {
        this.diaDelOrAdd = true
        this.dialogFormVisible = true
      }
      this.clean()
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    edgeDialog(row) {
      this.diaDelOrAdd = false
      this.dialogFormVisible = true
      this.addForm = {
        id: row.id,
        name: row.name,
        description: row.description
      }
      this.$nextTick(() => {
        this.$refs.addForm.clearValidate()
      })
    },
    createData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await createRole(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.clean()
            this.$message({
              type: 'success',
              message: '创建成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            console.log(res)
          }
        }
      })
    },
    updataData() {
      this.$refs.addForm.validate(async valid => {
        if (valid) {
          const res = await updateRole(this.addForm)
          if (res.code === 200) {
            this.dialogFormVisible = false
            this.$message({
              type: 'success',
              message: '编辑成功!',
              showClose: true,
              center: true,
              duration: 2 * 1000
            })
            this.getList()
          } else {
            this.dialogFormVisible = false
          }
        }
      })
    },
    // 删除
    deleteDialog(row) {
      this.dialogDelVisible = true
      this.deleteObiect = row
    },
    async deleteData() {
      const id = this.deleteObiect.id
      const res = await delRole(id)
      if (res.code === 200) {
        this.getList()
        this.dialogDelVisible = false
        this.$message({
          type: 'success',
          message: '删除成功!',
          showClose: true,
          center: true,
          duration: 2 * 1000
        })
      } else {
        this.dialogDelVisible = false
      }
    }
  }
}
</script>
<style lang="scss" scoped>
// .app-container{
//   .seatch{
//     width:180px;
//     height:32px;
//     border-radius:2px;
//     // border:1px solid rgba(35,189,234,0.7);
//     position: absolute;
//     right: 20px;
//     margin-top: -45px;
//   }
//   .content{
//     margin: 66px 0 0 0;
//   }
// }
</style>
